﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using FrameworkManapro.BusinessModel;

namespace GMAC.ReportingSystem.DataAccess
{
    public class GeneralParameterDataAccess
    {
        public Business.Entities.Configuration.ConfigurationParameterCollection GetConfigurationValues()
        {
            ArrayList parameters = new ArrayList() { };
            DataTable dt = DataAccessComponent.ExecuteSelect("procGeneralParameterSelect", parameters);
            if (dt == null || dt.Rows.Count == 0) return null;

            Business.Entities.Configuration.ConfigurationParameterCollection result = new Business.Entities.Configuration.ConfigurationParameterCollection();
            foreach (DataRow row in dt.Rows)
            {
                Business.Entities.Configuration.GeneralParameter value = new Business.Entities.Configuration.GeneralParameter()
                {
                    Key = row["GeneralKey"].ToString(),
                    Description = row["GeneralName"].ToString(),
                    DataType = Type.GetType(row["GeneralDataType"].ToString()),
                    Value = row["GeneralValue"].ToString(),
                    Status = (bool)row["GeneralStatus"]
                };
                if (row["GeneralLength"] != DBNull.Value)
                    value.Length = (short)row["GeneralLength"];
                if (row["GeneralMinValue"] != DBNull.Value)
                    value.MinValue = Convert.ChangeType(row["GeneralMinValue"], value.DataType);
                if (row["GeneralMaxValue"] != DBNull.Value)
                    value.MaxValue = Convert.ChangeType(row["GeneralMaxValue"], value.DataType);
                result.Add(value);
            }
            return result;
        }

        public bool SetConfigurationValues(Business.Entities.Configuration.ConfigurationParameterCollection values)
        {
            ArrayList parameters = new ArrayList();
            DataTable dt = new DataTable();
            dt.Columns.Add("Key");
            dt.Columns.Add("Value");
            DataRow row;
            foreach (Business.Entities.Configuration.GeneralParameter value in values)
            {
                row = dt.NewRow();
                row["Key"] = value.Key;
                row["Value"] = value.Value;
                dt.Rows.Add(row);
            }
            parameters.Add(dt);
            int result = (int)DataAccessComponent.ExecuteOperation("procGeneralParameterSave", parameters);
            if (result > 0)
                return true;

            return false;
        }
        public DataTable GetConfigurationValues(int GeneralID)
        {
            ArrayList parameters = new ArrayList() {GeneralID };
            return DataAccessComponent.ExecuteSelect("procGeneralParameterSelectID", parameters);
        }
    }
}
